#ifdef WIN32
#include <windows.h>
#endif

#include <GL/glew.h>
#include <GL/glu.h>
#include "world/Camera.h"

Camera::Camera(Object *parent) {
	this->parent = parent;
	target = NULL;
	up = btVector3(0, 1, 0);
	fieldOfView = 45.0f;
	aspect = 4.0f / 3.0f;
	viewNear = 0.1f;
	viewFar = 1000.0f;
}

// TODO: Add targeting back somehow.
void Camera :: view() {
	view(parent->getTransform());
}

void Camera :: view(const btTransform& transform) {
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(fieldOfView,
				   aspect,
				   viewNear,
				   viewFar);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	btVector3 origin = transform.getOrigin();
	btMatrix3x3 rotation(transform.getRotation().inverse());

	btVector3 lookAt = origin + rotation[2];
	btVector3 up = rotation[1];
	gluLookAt(origin.x(), origin.y(), origin.z(),
			  lookAt.x(), lookAt.y(), lookAt.z(),
			  up.getX(), up.getY(), up.getZ());
}


void Camera::clearTarget() {
	this->target = NULL;
}

Camera::~Camera(){
}
